// 日期格式化
export function parseTime(time, pattern) {

    if (arguments.length === 0 || !time) {
        return null
    }
    const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
            time = parseInt(time)
        } else if (typeof time === 'string') {
            time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
        }
        if ((typeof time === 'number') && (time.toString().length === 10)) {
            time = time * 1000
        }
        date = new Date(time)
    }

    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s)+}/g, (result, key) => {
        let value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        // if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return time_str
}

// 计算日期的差值
export function calculateMatchDuration(startDateTime, endDateTime) {


    let startDate = new Date();
    if(startDateTime != null)
        startDate = new Date(startDateTime);

     const endDate = new Date(endDateTime);

    // console.log(startDate)
    // console.log(endDate)

    // 计算时间差，得到毫秒数
    const durationMilliseconds = endDate.getTime() - startDate.getTime();

    if(durationMilliseconds <= 0)
        return "进行中"
    // console.log(durationMilliseconds)
    // 将毫秒数转换为天、小时、分钟和秒，并返回格式化后的字符串
    const days = Math.floor(durationMilliseconds / (1000 * 60 * 60 * 24));
    const hours = Math.floor((durationMilliseconds % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((durationMilliseconds % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((durationMilliseconds % (1000 * 60)) / 1000);

    let durationString = "";
    if (days > 0) {
        durationString += `${days}天`;
    }
    if (hours > 0) {
        durationString += `${hours}小时`;
    }
    if (minutes > 0) {
        durationString += `${minutes}分钟`;
    }
    if (seconds > 0) {
        durationString += `${seconds}秒`;
    }


    return durationString;
}

//得出比赛状态
export function getDateStatus(startDateTime, endDateTime){

    const startDate = new Date(startDateTime);
    const endDate = new Date(endDateTime);
    const now = new Date();
    if (now < startDate) { //比赛未开始
        return 0;
    } else if (now >= startDate && now <= endDate) { //比赛进行中
        return 1;
    } else { //比赛已结束
       return 2;
    }
}

// 秒转字符串
export function secondsMatch(durationMilliseconds,unit = 's') {

    let t = 1;
    if(unit == 's')
        t = 1;
    else if(unit == 'ms')
        t = 1000;
    // console.log(durationMilliseconds)
    // 将毫秒数转换为天、小时、分钟和秒，并返回格式化后的字符串
    const days = Math.floor(durationMilliseconds / (t * 60 * 60 * 24));
    const hours = Math.floor((durationMilliseconds % (t * 60 * 60 * 24)) / (t * 60 * 60));
    const minutes = Math.floor((durationMilliseconds % (t * 60 * 60)) / (t * 60));
    const seconds = Math.floor((durationMilliseconds % (t * 60)) / 1000);

    let durationString = "";
    if (days > 0) {
        durationString += `${days}天`;
    }
    if (hours > 0) {
        durationString += `${hours}小时`;
    }
    if (minutes > 0) {
        durationString += `${minutes}分钟`;
    }
    if (seconds > 0) {
        durationString += `${seconds}秒`;
    }

    return durationString;
}
